﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using road2.code;
using System.Collections.ObjectModel;
using road2.Model;

namespace road2.Views
{
    /// <summary>
    /// Interaction logic for RoadUserControl.xaml
    /// </summary>
    public partial class RoadUserControl : UserControl
    {
        public RoadUserControl()
        {
            InitializeComponent();
        }

        public RoadUserControl(RoadClass road, IEnumerable<CarClass> carCollection)
            : this()
        {
            this.DataContext = road;

            this.AddCars(carCollection);
        }

        public void AddCar(CarClass car)
        {
            LayoutRoot.Children.Add(new CarUserControl(car));
        }

        public void AddCars(IEnumerable<CarClass> cars)
        {
            foreach (var car in cars)
            {
                LayoutRoot.Children.Add(new CarUserControl(car));
            }
        }

        public void RemoveCar(CarClass car)
        {
            var cars = LayoutRoot.Children.OfType<CarUserControl>().Where(c => c.DataContext == car);

            if (cars.Count() != 0)
                LayoutRoot.Children.Remove(cars.First());
        }
    }
}
